package com.xej.seat.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xej.seat.mapper.TagMapper;
import com.xej.seat.service.TagCacheService;
import com.xej.seat.vo.Tag;

@Service
public class TagCacheServiceImpl implements TagCacheService, InitializingBean {
	
	@Autowired
	private TagMapper tagMapper;
	
	private static List<Tag> tags = new ArrayList<Tag>();
	
	private static Map<String, Tag> tagNameMap= new HashMap<String, Tag>();
	
	@SuppressWarnings("static-access")
	@Override
	public void afterPropertiesSet() throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("limit", 10);
		this.tags = this.queryTagByParams(params);
		for(Tag tag : this.tags) {
			tagNameMap.put(tag.getName(), tag);
		}
		System.out.println("--->" + tags);
		System.out.println("--->" + tagNameMap);
	}
	
	@Override
	public List<Tag> queryTagByParams(Map<String, Object> params) {
		return this.tagMapper.queryTagByParams(params);
	}
	
	@SuppressWarnings("static-access")
	@Override
	public double getPerCent(String tagName) {
		return this.tagNameMap.get(tagName) != null ? this.tagNameMap.get(tagName).getWeight() : 0;
	}
	
}
